# -*- coding:utf-8 -*-

# @File     :tasks
# @Author   :Yupeng
# @Date     :2018/11/30 11:10

from apps.storage.models.data import DataMeta
from .models import ClassCounter


def on_view(data_id):
    """
    用户访问数据详情页面时调用此函数
    :param data_id: 数据id
    :return: 无
    """
    data = DataMeta.objects.filter(pk=data_id).first()
    if data is None:
        raise RuntimeWarning('ObjectId ' + str(data_id) + ' not found')
    else:
        DataMeta.on_view(data_id)
        ClassCounter.get_unique(data.category).inc_view()


def on_download(data_id):
    """
    用户下载数据文件时调用此函数
    如果该数据拥有多个文件，只调用此函数一次
    :param data_id: 数据id
    :return: 无
    """
    data = DataMeta.objects.filter(pk=data_id).first()
    if data is None:
        raise RuntimeWarning('ObjectId ' + str(data_id) + ' not found')
    else:
        DataMeta.on_download(data_id)
        ClassCounter.get_unique(data.category).inc_dl()
